package cn.com.sd.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.sd.common.util.MyStringUtils;
import cn.com.sd.common.util.Page;
import cn.com.sd.log.LogConstant;
import cn.com.sd.log.model.ServiceLog;
import cn.com.sd.log.service.IServiceLogService;
import cn.com.sd.system.model.Operator;
import cn.com.sd.system.service.IOperatorService;
import cn.com.sd.system.service.IOrganizationService;


public class OperatorAction {

	HttpServletRequest request = ServletActionContext.getRequest();
	
	@Resource(name = "operatorService")
	private IOperatorService operatorService;
	
	@Resource(name = "organizationService")
	private IOrganizationService organizationService;
	
	@Resource(name = "serviceLogService")
	private IServiceLogService serviceLogService;
	
	private Operator operator;
	
	private Integer page;
	
	public String show() {
		
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("获取操作员列表");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		try {
			int count = operatorService.getOperatorCount();
			Page page = new Page(count, this.page);
			List<Operator> operators = operatorService.list(page);
			request.setAttribute("operatorList", operators);
			request.setAttribute("page", page);
			
			String result = "操作成功，获取总数为" + count + ",当前页是" + page.getCurrentPage();
			if (!CollectionUtils.isEmpty(operators)) {
				result += ",获取对象列表大小是" + operators.size();
			}
			serviceLog.setResult(result);
			serviceLogService.saveOrUpdate(serviceLog);
			
		} catch (Exception e) {
			serviceLog.setResult("操作失败," + e.getMessage());
			serviceLogService.saveOrUpdate(serviceLog);
		}
		
		return "show";
	}
	
	public String showAdd() {
		request.setAttribute("organizationList", organizationService.list(null));
		return "showAdd";
	}
	
	public String add() {
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("添加操作员信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		System.out.println(operator.getName()+"--"+operator.getEmail());
		String organizationId = request.getParameter("organizationId");
		if (!StringUtils.isEmpty(organizationId)) {
			operator.setOrganization(organizationService.detail(MyStringUtils.translateStringToInteger(organizationId)));
		}
		if (StringUtils.isEmpty(organizationId) || operator.getOrganization() == null) {
			serviceLog.setResult("没有所属组织，操作失败");
			serviceLogService.saveOrUpdate(serviceLog);
			return "showAdd";
		}
		
		try {
			boolean flag = operatorService.saveOrUpdate(operator);
			if (!flag) {
				request.setAttribute(WebCommon.ERR_MSG, "添加失败");
				serviceLog.setResult("信息保存失败");
				serviceLogService.saveOrUpdate(serviceLog);
				return "showAdd";
			}
			serviceLog.setResult("操作成功，操作员信息名称为" + operator.getName());
			serviceLogService.saveOrUpdate(serviceLog);
			
		} catch (Exception e) {
			serviceLog.setResult("操作失败," + e.getMessage());
			serviceLogService.saveOrUpdate(serviceLog);
		}
		
		return show();
	}
	
	public String detail() {
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("获取操作员信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) <= 0) {
			serviceLog.setResult("操作失败，id不合法");
			serviceLogService.saveOrUpdate(serviceLog);
			return "error";
		}
		Operator operator = operatorService.getDetail(MyStringUtils.translateStringToInteger(id));
		request.setAttribute("operator", operator);
		
		serviceLog.setResult("操作成功，获取对象为" + operator);
		serviceLogService.saveOrUpdate(serviceLog);
		
		return "detail";
	}
	
	public String del() {
		
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("删除操作员信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		String ids = request.getParameter("ids");
		if (StringUtils.isEmpty(ids)) {
			serviceLog.setResult("操作失败，id不合法");
			serviceLogService.saveOrUpdate(serviceLog);
			return "error";
		}
		
		List<Integer> idList = new ArrayList<Integer>();
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
				continue;
			}
			idList.add(MyStringUtils.translateStringToInteger(id));
		}
		if (CollectionUtils.isEmpty(idList)) {
			serviceLog.setResult("操作失败，id不合法");
			serviceLogService.saveOrUpdate(serviceLog);
			return "error";
		}
		boolean result = true;
		if (idList.size() == 1) {
			result = operatorService.del(idList.get(0));
		} else {
			result = operatorService.delBanch(idList);
		}
		if (!result) {
			request.setAttribute(WebCommon.ERR_MSG, "删除失败");
			serviceLog.setResult(StringUtils.join(idList, ",") + "操作员删除失败");
			serviceLogService.saveOrUpdate(serviceLog);
			return show();
		}
		serviceLog.setResult("操作成功，操作员" + operator.getName() + "删除成功");
		serviceLogService.saveOrUpdate(serviceLog);
		
		return show();
	}
	
	public String showSet() {
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("显示要编辑的操作员信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) <= 0) {
			serviceLog.setResult("操作失败，id不合法");
			serviceLogService.saveOrUpdate(serviceLog);
			return "error";
		}
		Operator operator = operatorService.getDetail(MyStringUtils.translateStringToInteger(id));
		request.setAttribute("operator", operator);
		request.setAttribute("organizationList", organizationService.list(null));
		
		serviceLog.setResult("操作成功，获取对象为" + operator);
		serviceLogService.saveOrUpdate(serviceLog);
		
		return "showSet";
	}
	
	public String set() {
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("编辑操作员信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		if (operator.getId() == null) {
			request.setAttribute(WebCommon.ERR_MSG, "添加失败");
			serviceLog.setResult("操作失败，id不合法");
			serviceLogService.saveOrUpdate(serviceLog);
			return "showSet";
		}
		System.out.println(operator.getName()+"--"+operator.getEmail());
		String organizationId = request.getParameter("organizationId");
		if (!StringUtils.isEmpty(organizationId)) {
			operator.setOrganization(organizationService.detail(MyStringUtils.translateStringToInteger(organizationId)));
		}
		if (StringUtils.isEmpty(organizationId) || operator.getOrganization() == null) {
			serviceLog.setResult("没有所属组织，操作失败");
			serviceLogService.saveOrUpdate(serviceLog);
			return "showSet";
		}
		
		try {
			boolean flag = operatorService.saveOrUpdate(operator);
			if (!flag) {
				request.setAttribute(WebCommon.ERR_MSG, "修改失败");
				serviceLog.setResult("信息保存失败");
				serviceLogService.saveOrUpdate(serviceLog);
				return "showSet";
			}
			serviceLog.setResult("操作成功，操作员信息名称为" + operator.getName());
			serviceLogService.saveOrUpdate(serviceLog);
			
		} catch (Exception e) {
			serviceLog.setResult("操作失败," + e.getMessage());
			serviceLogService.saveOrUpdate(serviceLog);
		}
		
		return show();
	}
	
	public String showPrivilege() {
		
		ServiceLog serviceLog = new ServiceLog(LogConstant.SYS_MODEL, LogConstant.OPERATOR_SERVICE);
		serviceLog.setContent("显示权限信息");
		Operator temp = (Operator) request.getSession().getAttribute(WebCommon.CURRENT_USER);
		serviceLog.setOperator(temp);
		serviceLog.setOperName(temp.getName());
		
		try {
			int count = operatorService.getOperatorCount();
			Page page = new Page(count, this.page);
			List<Operator> operators = operatorService.list(page);
			
			request.setAttribute("operatorList", operators);
			request.setAttribute("page", page);
			
			String result = "操作成功，获取总数为" + count + ",当前页是" + page.getCurrentPage();
			if (!CollectionUtils.isEmpty(operators)) {
				result += ",获取对象列表大小是" + operators.size();
			}
			serviceLog.setResult(result);
			serviceLogService.saveOrUpdate(serviceLog);
			
		} catch (Exception e) {
			serviceLog.setResult("操作失败," + e.getMessage());
			serviceLogService.saveOrUpdate(serviceLog);
		}
		return "showPrivilege";
	}
	
	public void setPrivilege() {
		
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator operator) {
		this.operator = operator;
	}
}

